let app = getApp();
const publicApi = require('../../ajax.js')
let getLoginValidCode = '/teacher/getLoginValidCode' //获取验证码
let loginSubmitInfo = '/teacher/login'  //登录
let codeToken;
Page({
  data: {
    codeImg: ''
  },
  // 页面加载获取验证码
  onLoad: function() {
    var that = this;
    publicApi.ajax('POST', {
      }, getLoginValidCode, (res) => {
      let codeBase64 = res.data.data.codeBase64
      codeToken = res.data.data.codeToken
      codeBase64 = codeBase64.replace(/\s*/g, "")
      that.setData({
        codeImg: codeBase64
      })
    })
  },
  // 切换验证码
  clickGetSms:function(){
    let that = this
    publicApi.ajax('POST',{
    }, getLoginValidCode,(res) => {
      let codeBase64 = res.data.data.codeBase64
      codeToken = res.data.data.codeToken
      codeBase64 = codeBase64.replace(/\s*/g, "")
      that.setData({
        codeImg: codeBase64
      })
    })
  },
  //提交用户登陆信息
  formSubmit: function (e) {
    let that = this
    let params = e.detail.value
    let lastLoginLatitude, lastLoginLongitude, lastLoginDevice;
    if (params.mobile == '' || params.password == '' || params.smsCode == ''){
      wx.showModal({
        title: '警告',
        content: '输入框不能为空',
        success(res) {
          if (res.confirm) {
           return false
          } else if (res.cancel) {
            return false
          }
        }
      })
    }
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        lastLoginLatitude = res.latitude
        lastLoginLongitude = res.longitude
        wx.getSystemInfo({
          success: function (res) {
            lastLoginDevice = res.model
            params.codeToken = codeToken
            params.lastLoginDevice = lastLoginDevice,
            params.lastLoginLatitude = lastLoginLatitude,
            params.lastLoginLongitude = lastLoginLongitude,
            console.log(params)
            that.userSubmit(params)
          }
        })
      }
    })

  },
  userSubmit: function (params){
    let that = this
    wx.request({
      url: 'https://organization-teacher.52carlife.com/org/teacher/login',
      method: 'POST',
      data: params,
      success: function (res) {
        console.log(res)
        console.log(res.data.data)
        wx.setStorage({
          key: 'teacherInfo',
          data: res.data.data,
        })
        app.globalData.teacherInfo = res.data.data
        if (res.data.status == 200) {
          wx.switchTab({
            url: '../tabBar/mainFunction/mainFunction',
          })
        }
        else {
          console.log(res.data.msg)
          wx.showModal({
            title: '警告',
            content: res.data.msg,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }

      },
      fail: function (res) {
        wx.showModal({
          title: '错误',
          content: res.data.msg,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
})